﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using System.Threading;

using MouseKeyboardLibrary;

namespace VSEnterRoom
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        void kh_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.S:
                    mh.Stop();
                    break;
                    
                case Keys.K:
                    Start();
                    break;

                case Keys.T:
                    th.Abort();
                    break;

            }

        }



        private KeyboardHook kh = new KeyboardHook();
        void mh_MouseMove(object sender, MouseEventArgs e)
        {
            x0 = e.X;
            y0 = e.Y;
        }

        private MouseHook mh = new MouseHook();
        private readonly int MOUSEEVENTF_LEFTDOWN = 0x2;
        private readonly int MOUSEEVENTF_LEFTUP = 0x4;
        [DllImport("user32")]
        public static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
        [DllImport("user32.dll")]
        static extern bool SetCursorPos(int X, int Y);

        private int x0, y0;
        private bool end = false;
        private void btn_Click(object sender, EventArgs e)
        {
            Start();
            // FF(null);
        }

        private void Start()
        {
            this.WindowState = FormWindowState.Minimized;
            this.Invoke(
                (Action)(() =>
                {
                    label5.Text = string.Format("{0},{1}", x0, y0);
                })
                );
            curveRowCount = int.Parse(textBox1.Text);
            EnterRoomTime = int.Parse(txtEnterRoomTime.Text);
            rowDelta = int.Parse(textBox2.Text);
            SecondP = new Point(int.Parse(textBox3.Text), int.Parse(textBox4.Text));
            mintime = int.Parse(textBox5.Text);
            th = new Thread(() =>
            {
                FF();
            });
            th.Start();
        }

        private Thread th;
        void FF()
        {
            while (!end)
            {
                for (int i = 0; i < curveRowCount; i++)
                {
                    EnterRoom(x0, y0 + i * rowDelta);
                }
                var st = mintime - curveRowCount * EnterRoomTime;
                if (st > 0)
                {
                    Thread.Sleep(st);
                }
            }
        }

        private int mintime = 10000;
        private int rowDelta = 20;
        private int curveRowCount = 3;
        public void EnterRoom(int x, int y)
        {
            Click(x, y);

            DoubleClick(x, y);
            Thread.Sleep(EnterRoomTime);
            Click(SecondP.X, SecondP.Y);
            Thread.Sleep(1000);
        }

        private int EnterRoomTime = 8000;
        public void DoubleClick(int x, int y)
        {
            //  SetCursorPos(x, y);
            Click(x, y);
            Thread.Sleep(80);
            Click(x, y);
        }
        public void Click(int x, int y)
        {
            SetCursorPos(x, y);
            Thread.Sleep(100);
            mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
            //    Thread.Sleep(20);
            mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
        }
        Point SecondP = new Point(800, 500);
        private void btn1_Click(object sender, EventArgs e)
        {
            //var f = new frmTarget();
            //f.ShowDialog();
            //x0 = f.x0;
            //y0 = f.y0;
            // MessageBox.Show(x0 + "," + y0);
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            end = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            mh.MouseMove += new MouseEventHandler(mh_MouseMove);
            mh.Start();

            kh.KeyDown += new KeyEventHandler(kh_KeyDown);
            kh.Start();
        }
    }
}
